{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Bitwise XOR of All Pairings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #brainteaser #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #脑筋急转弯 #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: xorAllNums"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #所有数对的异或和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;，两个数组都只包含非负整数。请你求出另外一个数组&nbsp;<code>nums3</code>&nbsp;，包含 <code>nums1</code>&nbsp;和 <code>nums2</code>&nbsp;中 <strong>所有数对</strong>&nbsp;的异或和（<code>nums1</code>&nbsp;中每个整数都跟 <code>nums2</code>&nbsp;中每个整数 <strong>恰好</strong>&nbsp;匹配一次）。</p>\n",
    "\n",
    "<p>请你返回 <code>nums3</code>&nbsp;中所有整数的 <strong>异或和</strong>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums1 = [2,1,3], nums2 = [10,2,5,0]\n",
    "<b>输出：</b>13\n",
    "<strong>解释：</strong>\n",
    "一个可能的 nums3 数组是 [8,0,7,2,11,3,4,1,9,1,6,3] 。\n",
    "所有这些数字的异或和是 13 ，所以我们返回 13 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums1 = [1,2], nums2 = [3,4]\n",
    "<b>输出：</b>0\n",
    "<strong>解释：</strong>\n",
    "所有数对异或和的结果分别为 nums1[0] ^ nums2[0] ，nums1[0] ^ nums2[1] ，nums1[1] ^ nums2[0] 和 nums1[1] ^ nums2[1] 。\n",
    "所以，一个可能的 nums3 数组是 [2,5,1,6] 。\n",
    "2 ^ 5 ^ 1 ^ 6 = 0 ，所以我们返回 0 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>0 &lt;= nums1[i], nums2[j] &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [bitwise-xor-of-all-pairings](https://leetcode.cn/problems/bitwise-xor-of-all-pairings/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [bitwise-xor-of-all-pairings](https://leetcode.cn/problems/bitwise-xor-of-all-pairings/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,1,3]\\n[10,2,5,0]', '[1,2]\\n[3,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        return (reduce(lambda x,y: x^y, [i for i in nums2]) if n1 % 2 else 0) ^ (reduce(lambda x,y: x^y, [i for i in nums1]) if n2 % 2 else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        # return reduce(lambda x,y: x^y, [i^j for i in nums1 for j in nums2])\n",
    "\n",
    "        # 2^i1  2 ^ i2 + ... + 2 ^ in\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        if n1 % 2 and n2 % 2:\n",
    "            return reduce(lambda x,y: x^y, [i for i in nums2]) ^ reduce(lambda x,y: x^y, [i for i in nums1])\n",
    "        elif n1 % 2:\n",
    "            return reduce(lambda x,y: x^y, [i for i in nums2])\n",
    "        elif n2 % 2:\n",
    "            return reduce(lambda x,y: x^y, [i for i in nums1])\n",
    "        else:\n",
    "            return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        # x 异或n次：偶数个 0;奇数个 x\n",
    "        ans = 0\n",
    "        if len(nums1) % 2: ans ^= reduce(xor, nums2)\n",
    "        if len(nums2) % 2: ans ^= reduce(xor, nums1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans=0\n",
    "        n1,n2=len(nums1),len(nums2)\n",
    "        if n1%2:\n",
    "            for i in nums2:\n",
    "                ans=ans^i\n",
    "        if n2%2:\n",
    "            for j in nums1:\n",
    "                ans=ans^j\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        if len(nums2) % 2 != 0:\n",
    "            for num in nums1:\n",
    "                a ^= num\n",
    "        if len(nums1) % 2 != 0:\n",
    "            for num in nums2:\n",
    "                b ^= num\n",
    "\n",
    "        return a ^ b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        m = len(nums1)\n",
    "        n = len(nums2)\n",
    "        if n%2 == 0:\n",
    "            ans ^= 0\n",
    "        else:\n",
    "            for x in nums1:\n",
    "                ans ^= x \n",
    "        if m %2 ==0:\n",
    "            ans ^= 0\n",
    "        else:\n",
    "            for y in nums2:\n",
    "                print(y)\n",
    "                ans ^= y\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        xor1 = 0\n",
    "        for num in nums1:\n",
    "            xor1 ^= num\n",
    "\n",
    "        xor2 = 0\n",
    "        for num in nums2:\n",
    "            xor2 ^= num\n",
    "\n",
    "        l1, l2 = len(nums1), len(nums2)\n",
    "        if l1 % 2 == 0 and l2 % 2 == 0:\n",
    "            return 0\n",
    "        if l1 % 2 == 0:\n",
    "            return xor1\n",
    "        if l2 % 2 == 0:\n",
    "            return xor2\n",
    "        \n",
    "        return xor1 ^ xor2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        if len(nums2) % 2 != 0:\n",
    "            for num in nums1:\n",
    "                a ^= num\n",
    "        if len(nums1) % 2 != 0:\n",
    "            for num in nums2:\n",
    "                b ^= num\n",
    "\n",
    "        return a ^ b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans=0\n",
    "        if len(nums2)%2:\n",
    "           for x in nums1:\n",
    "               ans^=x\n",
    "        if len(nums1)%2:\n",
    "           for x in nums2:\n",
    "               ans^=x\n",
    "        return ans                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        nor1, nor2 = 0, 0\n",
    "        for i in nums1:\n",
    "            nor1 = nor1 ^ i\n",
    "        for i in nums2:\n",
    "            nor2 = nor2 ^ i\n",
    "        ans = 0\n",
    "        if n1 % 2 == 1:\n",
    "            ans = ans ^ nor2\n",
    "        if n2 % 2 == 1:\n",
    "            ans = ans ^ nor1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n, m = len(nums1), len(nums2)\n",
    "        if n % 2 == 0 and m % 2 == 0:\n",
    "            return 0\n",
    "        elif n % 2 == 0:\n",
    "            return reduce(xor, nums1)\n",
    "        elif m % 2 == 0:\n",
    "            return reduce(xor, nums2)\n",
    "        else:\n",
    "            return reduce(xor, nums1+nums2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2);\n",
    "        # 计算出`nums1`, `nums2`数组的异或和\n",
    "        for v in nums2:b^=v;\n",
    "        for v in nums1:a^=v;\n",
    "        # 若`len(nums2) % 2 == 0`则`nums1`数组的`n`个异或和为0，否则为1，`nums2`同理\n",
    "        if n%2==0:a=0;\n",
    "        if m%2==0:b=0;\n",
    "        # 答案\n",
    "        return a^b;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2);\n",
    "        # 计算出`nums1`, `nums2`数组的异或和\n",
    "        for v in nums2:b^=v;\n",
    "        for v in nums1:a^=v;\n",
    "        # 若`len(nums2) % 2 == 0`则`nums1`数组的`n`个异或和为0，否则为1，`nums2`同理\n",
    "        if n%2==0:a=0;\n",
    "        if m%2==0:b=0;\n",
    "        # 答案\n",
    "        return a^b;\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m = len(nums1)\n",
    "        n = len(nums2)\n",
    "        result = 0\n",
    "        if(n % 2 == 1):\n",
    "            for num1 in nums1:\n",
    "                result ^= num1\n",
    "        if(m % 2 == 1):\n",
    "            for num2 in nums2:\n",
    "                result ^= num2\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans_1,ans_2=nums1[0],nums2[0]\n",
    "        n,m=len(nums1),len(nums2)\n",
    "        for i in range(1,n):\n",
    "            ans_1^=nums1[i]\n",
    "        for i in range(1,m):\n",
    "            ans_2^=nums2[i]\n",
    "        if n%2==0 and m%2==0:\n",
    "            return 0\n",
    "        elif n%2==0:\n",
    "            return ans_1\n",
    "        elif m%2==0:\n",
    "            return ans_2\n",
    "        else:\n",
    "            return ans_1^ans_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2);\n",
    "        # 计算出`nums1`, `nums2`数组的异或和\n",
    "        for v in nums2:b^=v;\n",
    "        for v in nums1:a^=v;\n",
    "        # 若`len(nums2) % 2 == 0`则`nums1`数组的`n`个异或和为0，否则为1，`nums2`同理\n",
    "        if n%2==0:a=0;\n",
    "        if m%2==0:b=0;\n",
    "        # 答案\n",
    "        return a^b;\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n, m = len(nums1), len(nums2)\n",
    "        xor = 0\n",
    "        if n & 1:\n",
    "            for x in nums2:\n",
    "                xor ^= x\n",
    "        if m & 1:\n",
    "            for x in nums1:\n",
    "                xor ^= x\n",
    "        return xor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums2) % 2: ans ^= reduce(xor, nums1)\n",
    "        if len(nums1) % 2: ans ^= reduce(xor, nums2)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        if len(nums1) % 2== 0 and len(nums2) % 2 == 0:\n",
    "            return 0\n",
    "        ans = 0\n",
    "        if len(nums2) % 2:\n",
    "            for x in nums1:\n",
    "                ans ^= x\n",
    "        if len(nums1) % 2:\n",
    "            for x in nums2:\n",
    "                ans ^= x\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        r1 = len(nums1) % 2\n",
    "        r2 = len(nums2) % 2\n",
    "        if r1 == 0 and r2 == 0:\n",
    "            return 0\n",
    "        elif r1 == 0 and r2 == 1:\n",
    "            return reduce(operator.xor, nums1, 0)\n",
    "        elif r1 == 1 and r2 == 0:\n",
    "            return reduce(operator.xor, nums2, 0)\n",
    "        else:\n",
    "            return reduce(operator.xor, nums1, 0) ^ reduce(operator.xor, nums2, 0)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, a: List[int], b: List[int]) -> int:\n",
    "        m,n=len(a),len(b)\n",
    "        ans=0\n",
    "        if m%2==1:\n",
    "            for i in b:\n",
    "                ans^=i\n",
    "        if n%2==1:\n",
    "            for i in a:\n",
    "                ans^=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        res = 0\n",
    "        if len(nums2) % 2 == 1:\n",
    "            for x in nums1:\n",
    "                res = res ^ x\n",
    "        if len(nums1) % 2 == 1:\n",
    "            for x in nums2:\n",
    "                res = res ^ x\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a1 = 0\n",
    "        a2 = 0\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        for i in range(n1):\n",
    "            a1 ^= nums1[i]\n",
    "        for i in range(n2):\n",
    "            a2 ^= nums2[i]\n",
    "        # print(a1, a2)\n",
    "        res = 0\n",
    "        if n1 & 1:\n",
    "            res ^= a2\n",
    "        if n2 & 1:\n",
    "            res ^= a1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        ans = 0\n",
    "        if n1 % 2 == 1:\n",
    "            for a in nums2:\n",
    "                ans ^= a\n",
    "        if n2 % 2 == 1:\n",
    "            for b in nums1:\n",
    "                ans ^= b\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums2) % 2: ans ^= reduce(xor, nums1)\n",
    "        if len(nums1) % 2: ans ^= reduce(xor, nums2)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m, n = len(nums1), len(nums2)\n",
    "\n",
    "        nums1_res = nums1[0]\n",
    "\n",
    "        for i in range(1, m):\n",
    "            nums1_res ^= nums1[i]\n",
    "        \n",
    "        nums2_res = nums2[0]\n",
    "        for i in range(1, n):\n",
    "            nums2_res ^= nums2[i]\n",
    "\n",
    "        return (nums1_res * (n % 2 != 0)) ^ (nums2_res * (m % 2 != 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m = len(nums1) % 2\n",
    "        n = len(nums2) % 2\n",
    "\n",
    "        ans = 0\n",
    "        if m == 1:\n",
    "            for num in nums2:\n",
    "                ans ^= num\n",
    "        if n == 1:\n",
    "            for num in nums1:\n",
    "                ans ^= num\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n = len(nums1)\n",
    "        m = len(nums2)\n",
    "        res = 0\n",
    "        if m%2==1:  \n",
    "            for num1 in nums1:\n",
    "                res ^= num1 \n",
    "        if n%2==1:\n",
    "            for num2 in nums2:\n",
    "                res ^= num2\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        n1 = len(nums1)\n",
    "        n2 = len(nums2)\n",
    "        res=0\n",
    "        if n1%2==0 and n2%2==0:\n",
    "            return res\n",
    "        \n",
    "        if n1%2==1 and n2%2==1:\n",
    "            for i in nums1+nums2:\n",
    "                res = res^i\n",
    "\n",
    "        if n1%2==0:\n",
    "            for i in nums1:\n",
    "                res=res^i\n",
    "        \n",
    "        if n2%2==0:\n",
    "            for i in nums2:\n",
    "                res = res^i\n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a=len(nums1)\n",
    "        b=len(nums2)\n",
    "        if a%2==0 and b%2==0:\n",
    "            return 0\n",
    "        if a%2==1 and b%2==1:\n",
    "            res=0\n",
    "            for i in nums1+nums2:\n",
    "                res^=i\n",
    "            return res\n",
    "        if a%2==1:\n",
    "            nums1=nums2\n",
    "        res=0\n",
    "        for i in nums1:\n",
    "            res^=i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums1) % 2: ans ^= reduce(xor, nums2)\n",
    "        if len(nums2) % 2: ans ^= reduce(xor, nums1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        xors1 = 0\n",
    "        for x in nums1:\n",
    "            xors1 ^= x\n",
    "        xors2 = 0\n",
    "        for x in nums2:\n",
    "            xors2 ^= x\n",
    "        \n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        if n1 & 1 == 0 and n2 & 1 == 0:\n",
    "            return 0\n",
    "        elif n1 & 1 == 0:\n",
    "            return xors1\n",
    "        elif n2 & 1 == 0:\n",
    "            return xors2\n",
    "        else:\n",
    "            return xors1 ^ xors2\n",
    "            \n",
    "            \n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2);\n",
    "        # 计算出`nums1`, `nums2`数组的异或和\n",
    "        for v in nums2:b^=v;\n",
    "        for v in nums1:a^=v;\n",
    "        # 若`len(nums2) % 2 == 0`则`nums1`数组的`n`个异或和为0，否则为1，`nums2`同理\n",
    "        if n%2==0:a=0;\n",
    "        if m%2==0:b=0;\n",
    "        # 答案\n",
    "        return a^b;\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2);\n",
    "        # 计算出`nums1`, `nums2`数组的异或和\n",
    "        for v in nums2:b^=v;\n",
    "        for v in nums1:a^=v;\n",
    "        # 若`len(nums2) % 2 == 0`则`nums1`数组的`n`个异或和为0，否则为1，`nums2`同理\n",
    "        if n%2==0:a=0;\n",
    "        if m%2==0:b=0;\n",
    "        # 答案\n",
    "        return a^b;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2);\n",
    "        # 计算出`nums1`, `nums2`数组的异或和\n",
    "        for v in nums2:b^=v;\n",
    "        for v in nums1:a^=v;\n",
    "        # 若`len(nums2) % 2 == 0`则`nums1`数组的`n`个异或和为0，否则为1，`nums2`同理\n",
    "        if n%2==0:a=0;\n",
    "        if m%2==0:b=0;\n",
    "        # 答案\n",
    "        return a^b;\n",
    "\n",
    "#作者：全力以赴✨\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        or1 = or2 = 0\n",
    "        for i in nums1:\n",
    "            or1 ^= i\n",
    "        for i in nums2:\n",
    "            or2 ^= i\n",
    "        ans = 0\n",
    "        if len(nums1) % 2:\n",
    "            ans ^= or2\n",
    "        if len(nums2) % 2:\n",
    "            ans ^= or1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #copy 0x3\n",
    "        ans =0\n",
    "        if len(nums2) %2:\n",
    "            ans ^= reduce(xor,nums1)\n",
    "        if len(nums1)%2:\n",
    "            ans ^= reduce(xor,nums2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        len1 = len(nums1)\n",
    "        len2 = len(nums2)\n",
    "        if len2 % 2 != 0:\n",
    "            for num in nums1:\n",
    "                a ^= num\n",
    "        if len1 % 2 != 0:\n",
    "            for num in nums2:\n",
    "                b ^= num\n",
    "\n",
    "        return a ^ b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums1) % 2:\n",
    "            ans ^= reduce(xor, nums2)\n",
    "        if len(nums2) % 2:\n",
    "            ans ^= reduce(xor, nums1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2)\n",
    "        if n%2==0:a=0\n",
    "        else:\n",
    "            for v in nums1:a^=v\n",
    "        if m%2==0:b=0\n",
    "        else:\n",
    "            for v in nums2:b^=v\n",
    "        return a^b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2)\n",
    "        # 计算出`nums1`, `nums2`数组的异或和\n",
    "        if n%2==0:\n",
    "            a=0\n",
    "        else:\n",
    "            for v in nums1:a^=v\n",
    "        if m%2==0:\n",
    "            b=0\n",
    "        else:\n",
    "            for v in nums2:b^=v\n",
    "        return a^b\n",
    "#作者：全力以赴✨"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        res = 0\n",
    "        if len(nums1)%2 == 1:\n",
    "            for i in nums2:\n",
    "                res ^= i\n",
    "        if len(nums2)%2 == 1:\n",
    "            for i in nums1:\n",
    "                res ^= i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums1) & 1:\n",
    "            ans ^= reduce(xor, nums2)\n",
    "        if len(nums2) & 1:\n",
    "            ans ^= reduce(xor, nums1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums2) % 2 == 1:\n",
    "            for num in nums1:\n",
    "                ans ^= num\n",
    "        if len(nums1) % 2 == 1:\n",
    "            for num in nums2:\n",
    "                ans ^= num\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #思维题\n",
    "        m,n=len(nums1),len(nums2)\n",
    "        res1=reduce(xor,nums2) if m%2==1 else 0\n",
    "        res2=reduce(xor,nums1) if n%2==1 else 0\n",
    "        return res1^res2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        part1 = part2 = 0\n",
    "        if len(nums1) & 1:\n",
    "            part2 = reduce(operator.xor, nums2)\n",
    "        if len(nums2) & 1:\n",
    "            part1 = reduce(operator.xor, nums1)\n",
    "        return part1 ^ part2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        res = 0\n",
    "        if len(nums1) % 2 != 0:\n",
    "            #print(len(nums1))\n",
    "            for num in nums2:\n",
    "                res ^= num\n",
    "        if len(nums2) % 2 != 0:\n",
    "            #print(len(nums2))\n",
    "            for num in nums1:\n",
    "                res ^= num\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        res=0\n",
    "        if len(nums1)%2==0 and len(nums2)%2==0:\n",
    "            return 0\n",
    "        if len(nums1)%2==1 and len(nums2)%2==0:\n",
    "            for i in range(len(nums2)):\n",
    "                res^=nums2[i]\n",
    "        if len(nums1)%2==0 and len(nums2)%2==1:\n",
    "            for i in range(len(nums1)):\n",
    "                res^=nums1[i]\n",
    "        if len(nums1)%2==1 and len(nums2)%2==1:\n",
    "            for i in range(len(nums1)):\n",
    "                res^=nums1[i]\n",
    "            for i in range(len(nums2)):\n",
    "                res^=nums2[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        res=0\n",
    "        # 总结 ：长度是奇数 另一个数组对最后结果才有贡献\n",
    "        # nums1的贡献\n",
    "        if len(nums2)%2==1:\n",
    "            for num1 in nums1:\n",
    "                res^=num1\n",
    "        # nums2的贡献\n",
    "        if len(nums1)%2==1:\n",
    "            for num2 in nums2:\n",
    "                res^=num2\n",
    "        return res\n",
    "\n",
    "'''\n",
    "        对于 [a, b] 和 [c, d]\n",
    "res = (a^c)^(a^d)^(b^c)^(b^d)\n",
    "      = (a^a)^(b^b)^(c^c)^(d^d)\n",
    "      = 0\n",
    "\n",
    "对于 [a, b] 和 [c, d, e]\n",
    "res = (a^c)^(a^d)^(a^e)^(b^c)^(b^d)^(b^e)\n",
    "      = (a^a^a)^(b^b^b)^(c^c)^(d^d)^(e^e)\n",
    "      = a^b\n",
    "\n",
    "对于 [a,b,c] 和 [d, e]\n",
    "res = (a^d)^(a^e)^(b^d)^(b^e)^(c^d)^(c^e)\n",
    "      = (a^a)^(b^b)^(c^c)^(d^d^d)^(e^e^e)\n",
    "      = d^e\n",
    "    \n",
    "如果 nums1 数组长度是偶数 ，nums2 中元素最终都会消为 0\n",
    "nums1[i]在nums3出现的次数是len(nums2),如果len(nums2)是偶数，nums1不贡献，异或的异或值为0，考虑nums2的贡献即可，如果是奇数，就是nums1[i]所以值的异或\n",
    "\n",
    "如果 nums1 数组长度是奇数，nums2 会剩下 其每个元素的异或和\n",
    "nums2[j]在nums3出现的次数是len(nums1)，同理\n",
    "\n",
    "对 num1 和 num2 应用以上发现：\n",
    "\n",
    "对于 [a, b] 和 [c, d]，nums1 偶 nums2 偶，结果 = 0 ^ 0 = 0\n",
    "对于 [a, b] 和 [c, d, e]，nums1 偶 nums2 奇，结果 = 0 ^ nums1 每个元素的异或和 = a^b\n",
    "对于 [a, b, c] 和 [d, e]，nums1 奇 nums2 偶，结果 = nums2 每个元素的异或和 ^ 0 = d^e\n",
    "[a][b] 奇 奇 结果是a^b\n",
    "'''\n",
    "\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a,b,m,n=0,0,len(nums1),len(nums2)\n",
    "        if n%2==0:\n",
    "            a=0\n",
    "        else:\n",
    "            for v in nums1:a^=v\n",
    "        if m%2==0:\n",
    "            b=0\n",
    "        else:\n",
    "            for v in nums2:b^=v\n",
    "        return a^b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums2) %2 ==1: ans ^= reduce(lambda a,b : a ^b, nums1)\n",
    "        if len(nums1) %2 ==1: ans ^= reduce(lambda a,b : a^b , nums2)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        r1 = len(nums1) % 2\n",
    "        r2 = len(nums2) % 2\n",
    "        if r1 == 0 and r2 == 0:\n",
    "            return 0\n",
    "        elif r1 == 0 and r2 == 1:\n",
    "            return reduce(lambda x, y: x^y, nums1, 0)\n",
    "        elif r1 == 1 and r2 == 0:\n",
    "            return reduce(lambda x, y: x^y, nums2, 0)\n",
    "        else:\n",
    "            return reduce(lambda x, y: x^y, nums1, 0) ^ reduce(lambda x, y: x^y, nums2, 0)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums2) % 2: ans ^= reduce(xor, nums1)\n",
    "        if len(nums1) % 2: ans ^= reduce(xor, nums2)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "# File       : Q2425.py\n",
    "# Time       ：2023/8/14 9:07\n",
    "# Author     ：author name\n",
    "# version    ：python 3\n",
    "# Description：\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"\n",
    "给你两个下标从 0 开始的数组 nums1 和 nums2 ，两个数组都只包含非负整数。请你求出另外一个数组 nums3 ，包含 nums1 和 nums2 中 所有数对 的异或和（nums1 中每个整数都跟 nums2 中每个整数 恰好 匹配一次）。\n",
    "\n",
    "请你返回 nums3 中所有整数的 异或和 。\n",
    "\"\"\"\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        key1 = 0\n",
    "        key2 = 0\n",
    "        for k in nums1:\n",
    "            key1 ^= k\n",
    "        for k in nums2:\n",
    "            key2 ^= k\n",
    "        if len(nums1) % 2 == 0 and len(nums2) % 2 == 0:\n",
    "            return 0\n",
    "        elif len(nums1) % 2 == 1 and len(nums2) % 2 == 1:\n",
    "            return key1 ^ key2\n",
    "        elif len(nums1) % 2 == 1:\n",
    "            return key2\n",
    "        return key1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        or1 = or2 = 0\n",
    "        if len(nums2) % 2:\n",
    "            for i in nums1:\n",
    "                or1 ^= i\n",
    "        if len(nums1) % 2:\n",
    "            for i in nums2:\n",
    "                or2 ^= i\n",
    "        return or1 ^ or2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m,n = len(nums1),len(nums2)\n",
    "\n",
    "        cnt1, cnt2 = m%2,n%2\n",
    "        xor1 = xor2=0\n",
    "        for x in nums1:\n",
    "            xor1 ^= x\n",
    "        for x in nums2:\n",
    "            xor2 ^=x\n",
    "        if cnt1 and cnt2:\n",
    "            return xor1 ^ xor2\n",
    "        elif cnt1:\n",
    "            return xor2\n",
    "        elif cnt2:\n",
    "            return xor1\n",
    "        return  0\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        p2 = reduce(lambda x, y:x ^y, nums2)\n",
    "\n",
    "        if len(nums1) % 2 == 0:\n",
    "            p2 = 0\n",
    "\n",
    "        if len(nums2) % 2 == 0:\n",
    "            return p2\n",
    "        else:\n",
    "            return reduce(lambda x, y:x^y, nums1) ^ p2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from functools import reduce\n",
    "from operator import xor\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums2) % 2: ans ^= reduce(xor, nums1)\n",
    "        if len(nums1) % 2: ans ^= reduce(xor, nums2)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums2) % 2 == 0 and len(nums1) % 2 == 0:\n",
    "            ans = 0\n",
    "        elif len(nums2) % 2 == 0:\n",
    "            for i in range(len(nums2)):\n",
    "                ans ^= nums2[i]\n",
    "        elif len(nums1) % 2 == 0:\n",
    "            for i in range(len(nums1)):\n",
    "                ans ^= nums1[i]\n",
    "        else:\n",
    "            for i in range(len(nums1)):\n",
    "                ans ^= nums1[i]\n",
    "            for i in range(len(nums2)):\n",
    "                ans ^= nums2[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        m, n = len(nums1), len(nums2)\n",
    "        def xor(nums):\n",
    "            tmp = 0\n",
    "            for x in nums:\n",
    "                tmp ^= x\n",
    "            return tmp\n",
    "        return (n & 1) * xor(nums1) ^ (m & 1) * xor(nums2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        def xor(nums):\n",
    "            tmp = 0\n",
    "            for x in nums:\n",
    "                tmp ^= x\n",
    "            return tmp\n",
    "        return (len(nums2) & 1) * xor(nums1) ^ (len(nums1) & 1) * xor(nums2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        cnt = [[0] * 30, [0] * 30]\n",
    "        N = [nums1, nums2]\n",
    "        for i in range(2):\n",
    "            for x in N[i]:\n",
    "                for j in range(30):\n",
    "                    cnt[i][j] += (1 << j & x) > 0\n",
    "        # print(cnt)\n",
    "        ret = 0\n",
    "        for i in range(30):\n",
    "            if cnt[0][i] * (len(nums2) - cnt[1][i]) & 1 != cnt[1][i] * (len(nums1) - cnt[0][i]) & 1:\n",
    "                ret |= 1 << i\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        #思维题(运用异或的性质)\n",
    "        m,n=len(nums1),len(nums2)\n",
    "        res1=reduce(xor,nums2) if m%2==1 else 0\n",
    "        res2=reduce(xor,nums1) if n%2==1 else 0\n",
    "        return res1^res2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        res=0\n",
    "        if len(nums1)%2==0 and len(nums2)%2==0:\n",
    "            return 0\n",
    "        if len(nums1)%2==1 and len(nums2)%2==0:\n",
    "            for i in range(len(nums2)):\n",
    "                res^=nums2[i]\n",
    "        if len(nums1)%2==0 and len(nums2)%2==1:\n",
    "            for i in range(len(nums1)):\n",
    "                res^=nums1[i]\n",
    "        if len(nums1)%2==1 and len(nums2)%2==1:\n",
    "            for i in range(len(nums1)):\n",
    "                res^=nums1[i]\n",
    "            for i in range(len(nums2)):\n",
    "                res^=nums2[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        n = len(nums1)\n",
    "        m = len(nums2)\n",
    "        for i in nums1:\n",
    "            a ^= i\n",
    "        for i in nums2:\n",
    "            b ^= i        \n",
    "        if n%2 and m%2:\n",
    "            return a^b\n",
    "        elif m%2==0 and n%2:\n",
    "            return b\n",
    "        elif m%2 and n%2==0:\n",
    "            return a\n",
    "        else:\n",
    "            return 0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        def xor(nums):\n",
    "            tmp = 0\n",
    "            for x in nums:\n",
    "                tmp ^= x\n",
    "            return tmp\n",
    "        return (len(nums2) & 1) * xor(nums1) ^ (len(nums1) & 1) * xor(nums2)\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        return (len(nums2) & 1) * reduce(xor, nums1) ^ (len(nums1) & 1) * reduce(xor, nums2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        nums1Len = len(nums1)\n",
    "        nums2Len = len(nums2)\n",
    "        x,y = 0,0\n",
    "        if (nums1Len%2!=0): x = reduce(xor,nums2)\n",
    "        if (nums2Len%2!=0): y = reduce(xor,nums1)\n",
    "        return x^y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "\n",
    "        def getL (arr):\n",
    "            ans = 0\n",
    "            obj = {}\n",
    "            for i, x in enumerate(arr):\n",
    "                if not x in obj:\n",
    "                    obj[x] = i\n",
    "                    ans += 1\n",
    "                else:\n",
    "                    ans -= 1\n",
    "                    del(obj[x])\n",
    "            return ans\n",
    "                    \n",
    "        l1 = len(nums1)\n",
    "        l2 = len(nums2)\n",
    "        ans1 = 0\n",
    "        for x in nums1: ans1 ^= x\n",
    "\n",
    "        ans2 = 0\n",
    "        for x in nums2: ans2 ^= x\n",
    "        print(l1, l2)\n",
    "        if l1 % 2 == 0 and l2 % 2 == 0:\n",
    "            return 0\n",
    "        if l1 % 2 == 1 and l2 % 2 == 0:\n",
    "            return ans2\n",
    "        if l2 % 2 == 1 and l1 % 2 == 0:\n",
    "            return ans1\n",
    "        return ans1 ^ ans2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:\n",
    "        ans = 0\n",
    "        if len(nums1) % 2:\n",
    "            for x in nums2:\n",
    "                ans ^= x\n",
    "        if len(nums2) % 2:\n",
    "            for x in nums1:\n",
    "                ans ^= x\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
